package com.exrobot.module.rbt.controller.admin.robotappversion;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.object.BeanUtils;
import static com.exrobot.framework.common.pojo.CommonResult.success;

import com.exrobot.framework.excel.core.util.ExcelUtils;

import com.exrobot.framework.apilog.core.annotation.ApiAccessLog;
import static com.exrobot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.exrobot.module.rbt.controller.admin.robotappversion.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robotappversion.RobotAppVersionDO;
import com.exrobot.module.rbt.service.robotappversion.RobotAppVersionService;

@Tag(name = "管理后台 - 机器人APP版本")
@RestController
@RequestMapping("/rbt/robot-app-version")
@Validated
public class RobotAppVersionController {

    @Resource
    private RobotAppVersionService robotAppVersionService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人APP版本")
    @PreAuthorize("@ss.hasPermission('rbt:robot-app-version:create')")
    public CommonResult<Long> createRobotAppVersion(@Valid @RequestBody RobotAppVersionSaveReqVO createReqVO) {
        return success(robotAppVersionService.createRobotAppVersion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人APP版本")
    @PreAuthorize("@ss.hasPermission('rbt:robot-app-version:update')")
    public CommonResult<Boolean> updateRobotAppVersion(@Valid @RequestBody RobotAppVersionSaveReqVO updateReqVO) {
        robotAppVersionService.updateRobotAppVersion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人APP版本")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rbt:robot-app-version:delete')")
    public CommonResult<Boolean> deleteRobotAppVersion(@RequestParam("id") Long id) {
        robotAppVersionService.deleteRobotAppVersion(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除机器人APP版本")
                @PreAuthorize("@ss.hasPermission('rbt:robot-app-version:delete')")
    public CommonResult<Boolean> deleteRobotAppVersionList(@RequestParam("ids") List<Long> ids) {
        robotAppVersionService.deleteRobotAppVersionListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人APP版本")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rbt:robot-app-version:query')")
    public CommonResult<RobotAppVersionRespVO> getRobotAppVersion(@RequestParam("id") Long id) {
        RobotAppVersionDO robotAppVersion = robotAppVersionService.getRobotAppVersion(id);
        return success(BeanUtils.toBean(robotAppVersion, RobotAppVersionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人APP版本分页")
    @PreAuthorize("@ss.hasPermission('rbt:robot-app-version:query')")
    public CommonResult<PageResult<RobotAppVersionRespVO>> getRobotAppVersionPage(@Valid RobotAppVersionPageReqVO pageReqVO) {
        PageResult<RobotAppVersionDO> pageResult = robotAppVersionService.getRobotAppVersionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotAppVersionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人APP版本 Excel")
    @PreAuthorize("@ss.hasPermission('rbt:robot-app-version:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRobotAppVersionExcel(@Valid RobotAppVersionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotAppVersionDO> list = robotAppVersionService.getRobotAppVersionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人APP版本.xls", "数据", RobotAppVersionRespVO.class,
                        BeanUtils.toBean(list, RobotAppVersionRespVO.class));
    }

}